import { defineStore } from 'pinia'
import { useExam } from './exam.js'
import { getCommentList, getCommentAdd, getCommentDel } from '@/service/exam'
import { ElMessage } from 'element-plus'

export const userEditorStore = defineStore('EditorStore', {
  state: () => ({
    current: '',
    pid: 0,
    maskTitle: '',
    currentCommitNode: {},
    referenceAnalysisComments: {
      notesList: [],
      discussionCommentsList: [],
      videoList: [],
      commitList: [], //参考解析
    }, //参考解析评论
    notesList: [], //笔记列表，
    /**
 *0  参考解析我要提问
  1  参考解析的追问
   2  我的笔记
   3  视频解析部分写笔记
   4  视频解析部分评论回复
   5 x友讨论，我也要提供解析
   6  x友评论我也要参与
   7. x友 评论回复
 */
    htmlContent: '',
    EditorComponents: {
      0: false,
      1: false,
      2: false,
      3: false,
      4: false,
      5: false,
      6: false,
      7: false,
    },
    list: {
      0: [],
      1: [],
      2: [],
      3: [],
      4: [],
      5: [],
      6: [],
      7: [],
    },
  }),
  actions: {
    updateShowAll(commentId) {
      this.referenceAnalysisComments.commitList.forEach((v) => {
        if (v.id === commentId) {
          v.replies.forEach((v1) => {
            v1.isVisible = true
          })
        }
      })
      console.log(this.referenceAnalysisComments.commitList)
    },
    handleUpdate({ key, value }) {
      this[key] = value
    },

    // 获取笔记信息
    async getCommentListFn(type) {
      const data = { logid: useExam().currQuestion.id, type }
      const res = await getCommentList(data)
      if (res.code === '000000') {
        this.list[type] = res.data
      }
    },

    // 删除item
    async handelDelComment(id, type) {
      const data = { act: 'isdelete', id, val: 1 }
      const res = await getCommentDel(data)
      if (res.code == '000000') {
        this.updateEditor(false)
        ElMessage.success('删除成功')
        this.getCommentListFn(type)
      }
    },

    async handleCommSaveAricle(content, type) {
      if (!content) return

      const getText = (str) => {
        return str
          .replace(/<[^<p>]+>/g, "") // 将所有<p>标签 replace ''
          .replace(/<[</p>$]+>/g, "") // 将所有</p>标签 replace ''
          .replace(/&nbsp;/gi, "") // 将所有 空格 replace ''
          .replace(/<[^<br/>]+>/g, ""); // 将所有 换行符 replace ''
      };
      const isNull = (str) => {
        if (str == "") return true;
        var regu = "^[ ]+$";
        var re = new RegExp(regu);
        return re.test(str);
      };

      if(isNull(getText(content))) {
        ElMessage.error('内容不可为空！')
        this.updateEditor(true)
        return
      }

      const data = {
        content,
        logid: useExam().currQuestion.id,
        pid: this.pid,
        subject: useExam().examInfo.subject,
        type: this.current,
      }
      const addRes = await getCommentAdd(data)
      if (addRes.code === '000000') {
        ElMessage.success('发布成功')
        this.updateEditor(false)
        this.resetEditor()
        this.getCommentListFn(type)
      }
    },
    updateEditor(flag) {
      this.EditorComponents[this.current] = flag
    },
    resetEditor(){
      this.current = 0
      this.pid = 0
      this.maskTitle = ''
    }
  },
})
